package com.zoe.thread.th14_volatile测试;

/**
 * @Author Zoe
 * @Date: 2021/05/07
 * @Description 单例模式
 */
public class VolatileSingleDemo {
    private static volatile VolatileSingleDemo volatileSingleDemo = null;

    private VolatileSingleDemo() {
        System.out.println("初始化");
    }

    public static VolatileSingleDemo getInstance() {
        if (volatileSingleDemo == null) {
            synchronized (VolatileSingleDemo.class) {
                if (volatileSingleDemo == null) {
                    volatileSingleDemo = new VolatileSingleDemo();
                }
            }
        }
        return volatileSingleDemo;
    }
}

class VolatileRun {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                System.out.println(VolatileSingleDemo.getInstance() == VolatileSingleDemo.getInstance());
            }).start();
        }
    }
}
